package com.hwadee.ems.service.impl;

import jakarta.annotation.PostConstruct;

import java.util.List;

import org.springframework.stereotype.Service;

import com.hwadee.ems.domain.dto.PageQuery;
import com.hwadee.ems.domain.dto.Result;
import com.hwadee.ems.domain.po.Textbook;
import com.hwadee.ems.domain.vo.TextbookVO;
import com.hwadee.ems.mapper.TextbookMapper;
import com.hwadee.ems.service.ITextbookService;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
/**
 * <p>
 * 教材 服务实现类
 * </p>
 *
 * @author EnosElinsa
 * @since 2024/06/21
 */
@Service
public class TextbookServiceImpl extends ServiceImpl<TextbookMapper, Textbook> implements ITextbookService {

    private static ITextbookService instance;

    public static ITextbookService getInstance() {
        return instance;
    }

    @PostConstruct
    void init() {
        instance = this;
    }

    @Override
    public Result<IPage<TextbookVO>> page(PageQuery pageQuery, TextbookVO textbookVO) {
        List<TextbookVO> vos = baseMapper.selectPageByConditions(
            textbookVO.getCourseName(),
            textbookVO.getIsbn(),
            textbookVO.getName(),
            textbookVO.getPublisher(),
            pageQuery.getSize(),
            (pageQuery.getCurrent() - 1) * pageQuery.getSize()
        );
        IPage<TextbookVO> page = Page.of(pageQuery.getCurrent(), pageQuery.getSize());
        page.setTotal(count());
        page.setRecords(vos);
        return Result.ok(page);
    }

}
